home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / calc / unitfrac.cal < prev    next >
Text File  |  1995-07-17  |  839b  |  36 lines

  1. /*
  2.  * Copyright (c) 1993 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Represent a fraction as sum of distinct unit fractions.
  7.  * The output is the unit fractions themselves, and in square brackets,
  8.  * the number of digits in the numerator and denominator of the value left
  9.  * to be found.  Numbers larger than 3.5 become very difficult to calculate.
  10.  */
  11.  
  12. define unitfrac(x)
  13. {
  14.     local    d, di, n;
  15.  
  16.     if (x <= 0)
  17.         quit "Non-positive argument";
  18.     d = 2;
  19.     do {
  20.         n = int(1 / x) + 1;
  21.         if (n > d)
  22.             d = n;
  23.         di = 1/d;
  24.         print '  [': digits(num(x)): '/': digits(den(x)): ']',, di;
  25.         x -= di;
  26.         d++;
  27.     } while ((num(x) > 1) || (x == di) || (x == 1));
  28.     print '  [1/1]',, x;
  29. }
  30.  
  31.  
  32. global lib_debug;
  33. if (lib_debug >= 0) {
  34.     print "unitfrac(x) defined";
  35. }
  36.